// pages/eventdetails/eventdetails.js
var util = require('../../utils/util.js');
var commonService = require('../../services/commonService.js');
var activityService = require('../../services/activityService.js');
var WxParse = require('../../wxParse/wxParse.js');
//获取应用实例
var app = getApp()
Page({
  data:{
    isDone:false,
  },
  onLoad:function(options){
    var id = options.id;
    var that = this;
    if (options.join != undefined){
      that.setData({
        join:true
      })
    }
    activityService.getActivityDetail({
      id:id,
      success:function(res){
        
        var data = res
          // date.replace(/-/g, "/")
        data.active_time_start = util.makeDateTimeToPretty(new Date(data.active_time_start.replace(/-/g, "/")), true);
        data.active_time_end = util.makeDateTimeToPretty(new Date(data.active_time_end.replace(/-/g, "/")), true);
        data.remainJoinNum = data.volunteer_num - data.join_num;
        that.setData({
          mainInfo: data,
          activity_id : id
        })
        WxParse.wxParse('description', 'html', that.data.mainInfo.description, that, 5);
      }
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  joinActivity:function(){
    if (!app.globalData.sysUserInfo){
      util.checkPermission(this, {});
      return false;
    }
    var that = this;
    if(that.data.mainInfo.status != 2){
      if (that.data.mainInfo.status == 1){
        wx.showToast({
          title: '活动报名未开始',
          icon: 'loading'
        })
        return false;
      }
      if (that.data.mainInfo.status == 3) {
        wx.showToast({
          title: '活动报名已结束',
          icon: 'loading'
        })
        return false;
      }
      if (that.data.mainInfo.status == 4) {
        wx.showToast({
          title: '活动已结束',
          icon: 'loading'
        })
        return false;
      }
    }
    if (that.data.mainInfo.join_num >= that.data.mainInfo.volunteer_num){
      wx.showToast({
        title: '活动报名人数已满',
        icon: 'loading'
      })
      return false;
    }
    activityService.joinActivity({
      user_id: app.globalData.sysUserInfo.userId,
      city_id: app.globalData.currentCityId,
      activity_id: that.data.activity_id,
      success:function(res){
        that.setData({
          isDone:true
        })
        wx.showToast({
          title: '申请已提交,请等待审核',
          success:function(){
            setTimeout(function () {
              wx.navigateBack()
            }, 1000)
          }
        })
      }
    })
  },
  activitySignIn:function(){
    var that = this;
    
    activityService.activitySignIn({
      id: that.data.mainInfo.signStatusAll.id,
      success: function (res) {
        wx.showToast({
          title: '签到成功!',
          success:function(res){
            that.data.mainInfo.signStatusAll.sign_status = 1;
            that.setData({
              mainInfo: that.data.mainInfo
            })
          }
        })
      }
    })
  },
  activitySignOut: function () {
    var that = this;
    
    activityService.activitySignOut({
      id: that.data.mainInfo.signStatusAll[0].id,
      success: function (res) {
        wx.showToast({
          title: '签退成功!',
          success: function (res) {
            that.data.mainInfo.signStatusAll[0].signout_status = 1;
            that.setData({
              mainInfo: that.data.mainInfo
            })
          }
        })
      }
    })
  },
  backTopPrevNavigate: function () {
    wx.navigateBack()
  }
})